package com.fr.report.web.event;

import com.fr.base.FCloneable;
import com.fr.base.Nameable;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONFunction;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.json.JSONVariable;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.js.JavaScript;
import com.fr.web.Repository;
import java.io.Serializable;

public class Listener
  implements XMLReadable, Serializable, Nameable, Cloneable
{
  public static final String XML_TAG = "Listener";
  private String targetWidget;
  private String eventName;
  private boolean invokeOnce;
  private JavaScript action;

  public Listener()
  {
    this.invokeOnce = false;
  }

  public boolean isDefault()
  {
    return false;
  }

  public Listener(String paramString)
  {
    this(paramString, null);
  }

  public Listener(String paramString, JavaScript paramJavaScript)
  {
    this(paramString, paramJavaScript, false);
  }

  public Listener(String paramString, JavaScript paramJavaScript, boolean paramBoolean)
  {
    this(null, paramString, paramJavaScript, paramBoolean);
  }

  public Listener(String paramString1, String paramString2, JavaScript paramJavaScript)
  {
    this(paramString1, paramString2, paramJavaScript, false);
  }

  public Listener(String paramString1, String paramString2, JavaScript paramJavaScript, boolean paramBoolean)
  {
    this.invokeOnce = false;
    setTargetWidget(paramString1);
    setName(paramString2);
    setAction(paramJavaScript);
    setInvokeOnce(paramBoolean);
  }

  public String getTargetWidget()
  {
    return this.targetWidget;
  }

  public void setTargetWidget(String paramString)
  {
    this.targetWidget = paramString;
  }

  public String getName()
  {
    return this.eventName;
  }

  public void setName(String paramString)
  {
    this.eventName = paramString;
  }

  public boolean isInvokeOnce()
  {
    return this.invokeOnce;
  }

  public void setInvokeOnce(boolean paramBoolean)
  {
    this.invokeOnce = paramBoolean;
  }

  public JavaScript getAction()
  {
    return this.action;
  }

  public void setAction(JavaScript paramJavaScript)
  {
    this.action = paramJavaScript;
  }

  public JSONObject createJSONConfig(Repository paramRepository)
    throws JSONException
  {
    JSONObject localJSONObject = new JSONObject();
    String str = getTargetWidget();
    if (StringUtils.isNotBlank(str))
      localJSONObject.put("target", new JSONVariable(str));
    localJSONObject.put("eventName", getName());
    localJSONObject.put("once", isInvokeOnce());
    localJSONObject.put("action", new JSONFunction(new String[] { "e" }, getAction().createJS(paramRepository)));
    return localJSONObject;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
    {
      String str;
      if ((str = paramXMLableReader.getAttr("target")) != null)
        setTargetWidget(str);
      if ((str = paramXMLableReader.getAttr("event")) != null)
        setName(str);
      if ((str = paramXMLableReader.getAttr("once")) != null)
        setInvokeOnce(Boolean.valueOf(str).booleanValue());
    }
    if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("JavaScript")))
      setAction(ReportXMLUtils.readJavaScript(paramXMLableReader));
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("Listener");
    if (StringUtils.isNotBlank(getTargetWidget()))
      paramXMLPrintWriter.attr("target", getTargetWidget());
    if (StringUtils.isNotBlank(getName()))
      paramXMLPrintWriter.attr("event", getName());
    if (isInvokeOnce())
      paramXMLPrintWriter.attr("once", isInvokeOnce());
    if (getAction() != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, getAction(), "JavaScript");
    paramXMLPrintWriter.end();
  }

  public String toString()
  {
    return this.eventName + ":" + this.action.getClass().getName();
  }

  public boolean equals(Object paramObject)
  {
    if (paramObject == this)
      return true;
    if (!(paramObject instanceof Listener))
      return false;
    return ((ComparatorUtils.equals(getTargetWidget(), ((Listener)paramObject).getTargetWidget())) || (ComparatorUtils.equals(getName(), ((Listener)paramObject).getName())) || (ComparatorUtils.equals(getAction(), ((Listener)paramObject).getAction())) || (isInvokeOnce() == ((Listener)paramObject).isInvokeOnce()));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    Listener localListener = (Listener)super.clone();
    if (this.action != null)
      localListener.action = ((JavaScript)this.action.clone());
    return localListener;
  }
}